{% if skip_init %}
FROM {{ base_image }}
{% else %}
# ================================================================
# START: Build Runtime Image from Scratch
# ================================================================
FROM {{ base_image }}

{% if 'ubuntu' in base_image and (base_image.endswith(':latest') or base_image.endswith(':24.04')) %}
{% set LIBGL_MESA = 'libgl1' %}
{% else %}
{% set LIBGL_MESA = 'libgl1-mesa-glx' %}
{% endif %}

# Install necessary packages and clean up in one layer
RUN apt-get update && \
    apt-get install -y wget sudo apt-utils {{ LIBGL_MESA }} libasound2-plugins git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Create necessary directories
RUN mkdir -p /opendevin && \
    mkdir -p /opendevin/logs && \
    mkdir -p /opendevin/poetry

ENV POETRY_VIRTUALENVS_PATH=/opendevin/poetry

RUN if [ ! -d /opendevin/miniforge3 ]; then \
    wget --progress=bar:force -O Miniforge3.sh "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" && \
    bash Miniforge3.sh -b -p /opendevin/miniforge3 && \
    rm Miniforge3.sh && \
    chmod -R g+w /opendevin/miniforge3 && \
    bash -c ". /opendevin/miniforge3/etc/profile.d/conda.sh && conda config --set changeps1 False && conda config --append channels conda-forge"; \
    fi

# Install Python and Poetry
RUN /opendevin/miniforge3/bin/mamba install conda-forge::poetry python=3.11 -y
# ================================================================
# END: Build Runtime Image from Scratch
# ================================================================
{% endif %}

# ================================================================
# START: Copy Project and Install/Update Dependencies
# ================================================================
RUN if [ -d /opendevin/code ]; then rm -rf /opendevin/code; fi
COPY ./code /opendevin/code

# Install/Update Dependencies
# 1. Install pyproject.toml via poetry
# 2. Install playwright and chromium
# 3. Clear poetry, apt, mamba caches
RUN cd /opendevin/code && \
    /opendevin/miniforge3/bin/mamba run -n base poetry env use python3.11 && \
    /opendevin/miniforge3/bin/mamba run -n base poetry install --only main,runtime --no-interaction --no-root && \
    apt-get update && \
    /opendevin/miniforge3/bin/mamba run -n base poetry run pip install playwright && \
    /opendevin/miniforge3/bin/mamba run -n base poetry run playwright install --with-deps chromium && \
    export OD_INTERPRETER_PATH=$(/opendevin/miniforge3/bin/mamba run -n base poetry run python -c "import sys; print(sys.executable)") && \
    {{ extra_deps }} {% if extra_deps %} && {% endif %} \
    /opendevin/miniforge3/bin/mamba run -n base poetry cache clear --all . && \
    {% if not skip_init %}chmod -R g+rws /opendevin/poetry && {% endif %} \
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    /opendevin/miniforge3/bin/mamba clean --all

# ================================================================
# END: Copy Project and Install/Update Dependencies
# ================================================================
